/*
 * @Author: hejianbo
 * @Date: 2024-01-09 16:01:01
 * @LastEditors: hejianbo
 * @FilePath: /js/login.js
 * @Description:
 */

$(() => {
  console.log("loaded");
  const BASE_URL = "https://yzbapitest.wanyih.com";
  const SERVICE_URL = "https://video.wanyih.com/agreement/server.html";
  const PRIVACY_URL = "https://video.wanyih.com/agreement/wep-yaoshi.html";
  const userInfo = {
    info: null,
    token: "",
    setInfoToken({ data, token }) {
      this.info = data;
      this.token = token;
      this.callBack();
    },
    callBack() {
      $(".login-wrap").hide();
      console.log("登录成功！", this.info.realName);
    },
  };

  $('.login-wrap .close').click(() => {
    $(".login-wrap").hide();
  })

  function MessagePop() {
    this.$msg = $(".login-wrap .msg");

    this.timer = null;
    this.show = function (text) {
      this.timer && clearTimeout(this.timer);
      this.$msg.text(text);
      this.$msg.show();
      this.timer = setTimeout(() => {
        this.$msg.hide();
        this.timer = null;
      }, 3000);
    };
  }

  function Loading() {
    this.$loading = $(".loading");
    this.timer = null;
    this.show = function () {
      this.timer && clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.$loading.show();
      }, 800);
    };

    this.hide = function () {
      this.timer && clearTimeout(this.timer);
      this.$loading.hide();
    };
  }

  function Request() {
    const _request = ({ type, url, data, success, error }) => {
      loadingIns.show();
      $.ajax({
        type,
        url: BASE_URL + url,
        dataType: "json",
        contentType: "application/json",
        data: type == "POST" ? JSON.stringify(data) : data,
        success: (res) => {
          loadingIns.hide();
          if (res.code != 200) {
            msgIns.show(res.msg || "服务出错！");
            error && error();
          } else {
            success(res);
          }
        },
        error: (res) => {
          loadingIns.hide();
          msgIns.show(res.msg || "服务出错！");
        },
      });
    };

    // 密码登录
    this.loginPwd = function (data, success, error) {
      return _request({
        type: "POST",
        url: "/v1/user/auth/doctor/open/login/pwd",
        data,
        success,
        error,
      });
    };

    // 获取短信验证码
    this.smsOpenCode = function (data, success, error) {
      return _request({
        type: "GET",
        url: "/v1/user/sms/open/code",
        data,
        success,
        error,
      });
    };

    // 短信验证码登录
    this.loginSms = function (data, success, error) {
      return _request({
        type: "POST",
        url: "/v1/user/auth/doctor/open/login/sms",
        data,
        success,
        error,
      });
    };

    // 找回密码
    this.pwdForget = function (data, success, error) {
      return _request({
        type: "POST",
        url: "/v1/user/auth/doctor/open/pwd/forget",
        data,
        success,
        error,
      });
    };
  }

  // 密码登录
  function PasswordLogin() {
    this.isShowPassword = false;
    this.isAgree = false;
    this.navigation = null;
    this.loginSuccess = null;

    this.initDom = function () {
      this.$parent = $(".passwordLogin");
      this.$mobileInput = this.$parent.find(".mobile");
      this.$passwordInput = this.$parent.find(".password");
      this.$eyes = this.$parent.find(".eyes");
      this.$selectIcon = this.$parent.find(".select-icon");
      this.$forgetBtn = this.$parent.find(".forget");
      this.$confirmBtn = this.$parent.find(".confirm");
      this.$toggleBtn = this.$parent.find(".toggle");
    };

    this.show = function () {
      this.$parent.show();
    };
    this.hide = function () {
      this.$parent.hide();
    };

    this.initEvent = function () {
      this.$parent.find(".service-btn").click(() => {
        window.open(SERVICE_URL);
      });
      this.$parent.find(".privacy-btn").click(() => {
        window.open(PRIVACY_URL);
      });
      this.$eyes.click(() => {
        this.isShowPassword = !this.isShowPassword;
        this.$eyes.find(".icon1").attr("src", this.isShowPassword ? "./imgs/show.png" : "./imgs/hide.png");
        this.$passwordInput.attr("type", this.isShowPassword ? "text" : "password");
      });

      this.$selectIcon.click(() => {
        this.isAgree = !this.isAgree;
        this.$selectIcon.find(".icon1").attr("src", this.isAgree ? "./imgs/selected.png" : "./imgs/select.png");
        this.$confirmBtn.toggleClass("disable");
      });

      this.$confirmBtn.click(() => {
        if (!this.isAgree) return msgIns.show("请勾选同意服务协议和隐私协议");

        const mobile = this.$mobileInput.val();
        const password = this.$passwordInput.val();
        if (!mobile) return msgIns.show("请输入手机号");
        if (!password) return msgIns.show("请输入密码");

        apis.loginPwd({ mobile, password }, (res) => {
          msgIns.show("登录成功!");
          loginSuccess(res);
        });
      });

      this.$toggleBtn.click(() => {
        this.navigation && this.navigation();
      });

      this.$forgetBtn.click(() => {
        this.forgetNav && this.forgetNav();
      });
    };

    this.setNavigation = function (v1, v2) {
      this.navigation = v1;
      this.forgetNav = v2;
    };

    this.setLoginSuccess = function (v) {
      this.loginSuccess = v;
    };

    this.initDom();
    this.initEvent();
  }

  // 验证码登录
  function YzmLogin() {
    this.isAgree = false;
    this.yzmDisable = false;
    this.timer = null;
    this.totalTime = 60;
    this.navigation = null;
    this.loginSuccess = null;

    this.show = function () {
      this.$parent.show();
    };
    this.hide = function () {
      this.endCountdown();
      this.$parent.hide();
    };

    this.initDom = function () {
      this.$parent = $(".yzmLogin");
      this.$mobileInput = this.$parent.find(".mobile");
      this.$smsInput = this.$parent.find(".sms");
      this.$yzmBtn = this.$parent.find(".yzm");
      this.$selectIcon = this.$parent.find(".select-icon");
      this.$confirmBtn = this.$parent.find(".confirm");
      this.$toggleBtn = this.$parent.find(".toggle");
    };

    this.startCountdown = function () {
      this.timer && clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.totalTime = this.totalTime - 1;
        this.$yzmBtn.text(this.totalTime + "s");
        if (this.totalTime <= 0) {
          this.endCountdown();
        }
      }, 1000);
      this.$yzmBtn.addClass("timing").text(this.totalTime + "s");
    };

    this.endCountdown = function () {
      clearInterval(this.timer);
      this.timer = null;
      this.totalTime = 60;
      this.$yzmBtn.removeClass("timing").text("获取验证码");
      this.yzmDisable = false;
    };

    this.initEvent = function () {
      this.$parent.find(".service-btn").click(() => {
        window.open(SERVICE_URL);
      });
      this.$parent.find(".privacy-btn").click(() => {
        window.open(PRIVACY_URL);
      });

      this.$selectIcon.click(() => {
        this.isAgree = !this.isAgree;
        this.$selectIcon.find(".icon1").attr("src", this.isAgree ? "./imgs/selected.png" : "./imgs/select.png");
        this.$confirmBtn.toggleClass("disable");
      });

      this.$yzmBtn.click(() => {
        if (this.yzmDisable) return;
        const mobile = this.$mobileInput.val();
        if (!mobile) return msgIns.show("请输入手机号！");

        apis.smsOpenCode({ mobile, codeType: "USER_LOGIN" }, ({ msg }) => {
          this.yzmDisable = true;
          msgIns.show(msg);
          this.startCountdown();
        });
      });

      this.$confirmBtn.click(() => {
        if (!this.isAgree) return msgIns.show("请勾选同意服务协议和隐私协议");

        const mobile = this.$mobileInput.val();
        const code = this.$smsInput.val();
        if (!mobile) return msgIns.show("请输入手机号");
        if (!code) return msgIns.show("请输入短信验证码");

        apis.loginSms({ mobile, code }, (res) => {
          msgIns.show("登录成功!");
          this.endCountdown();
          loginSuccess(res);
        });
      });

      this.$toggleBtn.click(() => {
        this.navigation && this.navigation();
      });
    };

    this.setNavigation = function (v) {
      this.navigation = v;
    };

    this.setLoginSuccess = function (v) {
      this.loginSuccess = v;
    };

    this.initDom();
    this.initEvent();
  }

  // 重置密码
  function PasswordReset() {
    this.isShowPassword = false;
    this.yzmDisable = false;
    this.timer = null;
    this.totalTime = 60;
    this.navigation = null;

    this.show = function () {
      this.$parent.show();
    };
    this.hide = function () {
      this.endCountdown();
      this.$parent.hide();
    };

    this.initDom = function () {
      this.$parent = $(".passwordReset");
      this.$mobileInput = this.$parent.find(".mobile");
      this.$smsInput = this.$parent.find(".sms");
      this.$yzmBtn = this.$parent.find(".yzm");
      this.$passwordInput = this.$parent.find(".password");
      this.$eyes = this.$parent.find(".eyes");
      this.$confirmBtn = this.$parent.find(".confirm");
      this.$toggleBtn = this.$parent.find(".toggle");
    };

    this.startCountdown = function () {
      this.timer && clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.totalTime = this.totalTime - 1;
        this.$yzmBtn.text(this.totalTime + "s");
        if (this.totalTime <= 0) {
          this.endCountdown();
        }
      }, 1000);
      this.$yzmBtn.addClass("timing").text(this.totalTime + "s");
    };

    this.endCountdown = function () {
      clearInterval(this.timer);
      this.timer = null;
      this.totalTime = 60;
      this.$yzmBtn.removeClass("timing").text("获取验证码");
      this.yzmDisable = false;
    };

    this.initEvent = function () {
      this.$eyes.click(() => {
        this.isShowPassword = !this.isShowPassword;
        this.$eyes.find(".icon1").attr("src", this.isShowPassword ? "./imgs/show.png" : "./imgs/hide.png");
        this.$passwordInput.attr("type", this.isShowPassword ? "text" : "password");
      });

      this.$yzmBtn.click(() => {
        if (this.yzmDisable) return;
        const mobile = this.$mobileInput.val();
        if (!mobile) return msgIns.show("请输入手机号！");

        apis.smsOpenCode({ mobile, codeType: "USER_PWD_RESET" }, ({ msg }) => {
          this.yzmDisable = true;
          msgIns.show(msg);
          this.startCountdown();
        });
      });

      this.$confirmBtn.click(() => {
        const mobile = this.$mobileInput.val();
        const code = this.$smsInput.val();
        const password = this.$passwordInput.val();
        if (!mobile) return msgIns.show("请输入手机号!");
        if (!code) return msgIns.show("请输入短信验证码!");
        if (!password) return msgIns.show("请输入新密码!");

        if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,16}$/.test(password)) {
          return msgIns.show("密码格式为8-16位的大小写字母、数字组合!");
        }

        apis.pwdForget({ mobile, code, password }, (res) => {
          msgIns.show("密码更新成功!");
          this.endCountdown();
        });
      });

      this.$toggleBtn.click(() => {
        this.navigation && this.navigation();
      });
    };

    this.setNavigation = function (v) {
      this.navigation = v;
    };

    this.initDom();
    this.initEvent();
  }

  const msgIns = new MessagePop();
  const loadingIns = new Loading();
  const apis = new Request();
  const passwordLoginIns = new PasswordLogin();
  const yzmLoginIns = new YzmLogin();
  const passwordResetIns = new PasswordReset();

  passwordLoginIns.setNavigation(
    () => {
      passwordLoginIns.hide();
      yzmLoginIns.show();
    },
    () => {
      passwordLoginIns.hide();
      passwordResetIns.show();
    }
  );
  yzmLoginIns.setNavigation(() => {
    yzmLoginIns.hide();
    passwordLoginIns.show();
  });
  passwordResetIns.setNavigation(() => {
    passwordResetIns.hide();
    passwordLoginIns.show();
  });
  
  const loginSuccess = (res) => {
    userInfo.setInfoToken(res);
  };
  passwordLoginIns.setLoginSuccess(loginSuccess);
  yzmLoginIns.setLoginSuccess(loginSuccess);

});
